package com.mlethe.library.multi.demo;

import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.mlethe.library.multi.api.MultiMedia;
import com.mlethe.library.multi.api.ShareAction;
import com.mlethe.library.multi.api.ShareScene;
import com.mlethe.library.multi.api.entity.ImageObject;
import com.mlethe.library.multi.api.entity.ShareImage;
import com.mlethe.library.multi.api.entity.ShareMini;
import com.mlethe.library.multi.api.entity.ShareMusic;
import com.mlethe.library.multi.api.entity.ShareVideo;
import com.mlethe.library.multi.api.entity.ShareWeb;
import com.mlethe.library.multi.api.listener.OnActionListener;
import com.mlethe.library.multi.demo.databinding.ActivityQqShareBinding;

import java.io.File;
import java.util.Map;

/**
 * QQ分享
 */
public class QQShareActivity extends AppCompatActivity {

    private ActivityQqShareBinding binding;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityQqShareBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        OnActionListener listener = new OnActionListener() {
            @Override
            public void onComplete(@NonNull MultiMedia media, @NonNull Map<String, String> map) {
                Log.e("multi", "onComplete: " + media + ", map->" + map);
                Toast.makeText(QQShareActivity.this, media + "分享完成：" + map, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onCancel(@NonNull MultiMedia media) {
                Log.e("multi", "onCancel: " + media);
                Toast.makeText(QQShareActivity.this, media + "分享取消", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFailure(@NonNull MultiMedia media, int code, String msg) {
                Log.e("multi", "onFailure: " + media + "，code:" + code + ", msg: " + msg);
                Toast.makeText(QQShareActivity.this, media + "分享失败，code:" + code + ", msg: " + msg, Toast.LENGTH_SHORT).show();
            }
        };
        binding.imgBtn.setOnClickListener(v -> {
            File externalFilesDir = getExternalFilesDir(null);
            ShareImage share = new ShareImage(new ImageObject(externalFilesDir + "/file_images/ccc.JPG"));
            new ShareAction(MultiMedia.QQ)
                    .setShare(share)
                    .setShareScene(getShareScene())
                    .setCallback(listener)
                    .share(this);
        });
        binding.webBtn.setOnClickListener(v -> {
            ShareWeb share = new ShareWeb("https://www.baidu.com");
            share.setTitle("分享网页");
            share.setDescription("分享网页测试描述");
            File externalCacheDir = getExternalCacheDir();
            share.setThumbImage(new ImageObject(externalCacheDir + "/file_images/eee.jpg"));
            new ShareAction(MultiMedia.QQ)
                    .setShare(share)
                    .setShareScene(getShareScene())
                    .setCallback(listener)
                    .share(this);
        });
        binding.videoBtn.setOnClickListener(v -> {
            File externalFilesDir = getExternalFilesDir(null);
            ShareVideo share = new ShareVideo(externalFilesDir + "/file_video/eeee.mp4");
            share.setTitle("分享视频");
            new ShareAction(MultiMedia.WECHAT)
                    .setShare(share)
                    .setShareScene(ShareScene.CIRCLE)
                    .setCallback(listener)
                    .share(this);
        });
        binding.miniBtn.setOnClickListener(v -> {
            ShareMini share = new ShareMini("gh_d43f693ca31f", "/pages/media");
            share.setTitle("小程序消息Title");
            share.setDescription("小程序消息Desc");
            File externalCacheDir = getExternalCacheDir();
            share.setThumbImage(new ImageObject(externalCacheDir + "/file_images/eee.jpg"));
            new ShareAction(MultiMedia.QQ)
                    .setShare(share)
                    .setShareScene(getShareScene())
                    .setCallback(listener)
                    .share(this);
        });
        binding.musicBtn.setOnClickListener(v -> {
            File externalCacheDir = getExternalCacheDir();
            ShareMusic share = new ShareMusic("http://xxx/xx.mp3", "http://www.qq.com/news/1.html");
            share.addThumbImage(new ImageObject(externalCacheDir + "/file_images/eee.jpg")); // 选填，专辑图本地文件路径
            share.setTitle("歌曲名称"); // 必填，不能为空
            share.setDescription("歌曲描述");  // 选填，建议与歌手名字段 singerName 保持一致
            share.setThumbImage(new ImageObject(externalCacheDir + "/file_images/eee.jpg"));
            new ShareAction(MultiMedia.QQ)
                    .setShare(share)
                    .setShareScene(ShareScene.FRIEND)
                    .setCallback(listener)
                    .share(this);
        });
    }

    /**
     * 获取分享的场景
     *
     * @return
     */
    private ShareScene getShareScene() {
        int id = binding.rgScene.getCheckedRadioButtonId();
        if (id == R.id.rb_friend) {
            return ShareScene.FRIEND;
        } else {
            return ShareScene.CIRCLE;
        }
    }
}
